import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _class;

/**
 * MenuImport - 导入客户化菜单
 * @date: 2019-8-19

 */
import React from 'react';
import { Upload } from 'antd';
import { Bind } from 'lodash-decorators';
import { Button as ButtonPermission } from 'components/Permission';
import { API_HOST, CPAAS_IAM } from 'utils/config';
import notification from 'utils/notification';
import { getAccessToken, getCurrentOrganizationId } from 'utils/utils';
import intl from 'utils/intl';
var viewMessagePrompt = 'ciam.menuConfig.view.message';
var MenuImport = (_dec = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(MenuImport, _React$Component);

  var _super = _createSuper(MenuImport);

  function MenuImport() {
    var _this;

    _classCallCheck(this, MenuImport);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      uploadLoading: false
    };
    return _this;
  }

  _createClass(MenuImport, [{
    key: "getUploadProps",
    value: function getUploadProps() {
      var _this2 = this;

      var _this$props$onReload = this.props.onReload,
          onReload = _this$props$onReload === void 0 ? function (e) {
        return e;
      } : _this$props$onReload;
      var organizationId = getCurrentOrganizationId();
      return {
        name: 'customMenuFile',
        accept: '.json',
        headers: {
          Authorization: "bearer ".concat(getAccessToken())
        },
        action: "".concat(API_HOST).concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/menus/custom-menu-import"),
        showUploadList: false,
        onChange: function onChange(_ref) {
          var file = _ref.file;
          var status = file.status,
              response = file.response;

          _this2.setState({
            uploadLoading: status === 'uploading'
          });

          if (status === 'done' && response === 'success') {
            notification.success({
              message: intl.get("".concat(viewMessagePrompt, ".importSuccess")).d('导入成功')
            });
            onReload();
          } else if (status === 'error') {
            notification.error({
              message: response.message
            });
          }
        },
        style: {
          margin: '0 12px'
        }
      };
    }
  }, {
    key: "render",
    value: function render() {
      var path = this.props.path;
      var uploadLoading = this.state.uploadLoading;
      var uploadProps = this.getUploadProps();
      return /*#__PURE__*/React.createElement(Upload, uploadProps, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.importMenu"),
          type: 'button',
          meaning: '菜单配置-导入客户化菜单'
        }],
        className: "label-btn",
        icon: "upload",
        loading: uploadLoading
      }, intl.get("".concat(viewMessagePrompt, ".title.importMenu")).d('导入客户化菜单')));
    }
  }]);

  return MenuImport;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "getUploadProps", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "getUploadProps"), _class.prototype)), _class));
export { MenuImport as default };